<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class RegionController extends AdminBaseController
{

    /**
     *
     */
    public function index()
    {

        $region = M('region')->where('pid=0')->select();
        $this->assign('region', $region);
        $this->display('setting/region');
    }
    /**
     * 添加地区
     */
    public function region_add()
    {

        if (IS_POST) {
            $insert           = array();
            $insert['name']   = $_POST['name'];
            $insert['region'] = $_POST['region'];
            if (!empty($_POST['pid1'])) {
                $insert['pid'] = $_POST['pid1'];
            } elseif (!empty($_POST['pid']) && empty($_POST['pid1'])) {
                $insert['pid'] = $_POST['pid'];
            }
            //print_r($insert);exit;
            $result = M('region')->add($insert);
            if ($result >= 0) {
                admin_log('添加区域' . '[' . $_POST['name'] . ']', null);
                $this->success('添加区域成功', 'index');exit;
            } else {
                $this->error('添加区域失败', 'index');exit;
            }
        }

        $this->display('setting/region_add');
    }
    //编辑地区
    public function region_edit()
    {

        $id = $_GET['id'];
        if (IS_POST) {
            $update           = array();
            $update['name']   = $_POST['name'];
            $update['region'] = $_POST['region'];
            if (!empty($_POST['pid1'])) {
                $update['pid'] = $_POST['pid1'];
            } elseif (!empty($_POST['pid']) && empty($_POST['pid1'])) {
                $update['pid'] = $_POST['pid'];
            }
            print_r($update);exit;
            $result = M('region')->where('id=' . $_POST['id'])->save($update);
            if ($result >= 0) {
                admin_log('编辑区域' . '[' . $_POST['name'] . ']', null);
                $this->success('编辑区域成功', 'index');exit;
            } else {
                $this->error('编辑区域失败', 'index');exit;
            }
        }

        $region = M('region')->where('id=' . $id)->find();
        if ($region['pid'] == 0) {
            $region['p_name'] = $region['name'];
        } else {
            $p_name           = M('region')->where('id=' . $region['pid'])->find();
            $region['p_name'] = $p_name['name'];

        }
        //print_r($region);exit;
        $this->assign('region', $region);
        $this->assign('id', $id);

        $this->display('setting/region_edit');
    }

    public function get_region()
    {
        $data = array('info' => '获取信息失败', 'status' => '0');
        $id   = I('pid');
        if (empty($id)) {
            $id = 0;
        }
        if (!isAJAX) {
            halt('请求有误');
        } else {

            $region = M('region')->where('pid=' . $id)->select();
            $data   = array('info' => '获取信息成功', 'status' => '1', 'region' => $region);
        }
        $this->ajaxReturn($data, 'json');
    }

    public function region_del()
    {
        $data = array('info' => '删除区域失败', 'status' => '0');
        $id   = I('id');

        if (!isAJAX) {
            halt('请求有误');
        } else {
            $pppid = getAllChildIds('region', 'pid', $id);
            //print_r($pppid);exit;
            if ($pppid) {
                $condition['id'] = array('in', $pppid);
                $result          = M('region')->where($condition)->delete();
                if ($result) {
                    $data = array('info' => '删除成功', 'status' => '1');
                } else {
                    $data = array('info' => '删除失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数错误', 'status' => '0');
            }

        }
        $this->ajaxReturn($data, 'json');
    }
    public function edit_name()
    {
        $data = array('info' => '编辑地区名称失败', 'status' => '0');
        $id   = I('id');
        $name = I('name');
        if (empty($id)) {
            $data = array('info' => '参数错误', 'status' => '0');
        } else {
            if (!isAJAX) {
                halt('请求有误');
            } else {
                $condition['id'] = $id;
                $update['name']  = $name;
                $region          = M('region')->where($condition)->save($update);
                if ($region >= 0) {
                    $data = array('info' => '修改信息成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改信息失败', 'status' => '0');
                }

            }
        }

        $this->ajaxReturn($data, 'json');
    }

}
